<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>品类元数据权限</title>
	<link rel="stylesheet" type="text/css" href="/webapp/css/easyui.css">
	<link rel="stylesheet" type="text/css" href="/webapp/css/icon.css">
	<link rel="stylesheet" type="text/css" href="/webapp/css/color.css">
	<link rel="stylesheet" type="text/css" href="/webapp/css/public.css">
	<script type="text/javascript" src="/webapp/js/lib/jquery.min.js"></script>
	<script type="text/javascript" src="/webapp/js/lib/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="/webapp/js/lib/jquery.global.settings.js"></script>
	<script type="text/javascript" src="/webapp/js/lib/ajaxfileupload.js"></script>
	<script type="text/javascript" src="/webapp/js/common/config.js"></script>
</head>
<body>
<div class="easyui-container-fluid">
	<div class="easyui-panel" style="border: none;">
		<div class="nav" id="nav"></div>
		<div class="easyui-panel" style="border:none;">
			<form id="ff" method="post">
				<div style="margin-bottom:20px">
					<label class="item-label">品类：</label>
					<select class="easyui-combobox" id="categorySelect" name="categoryCode" style="width:174px;" required="required" >
					</select>
				</div>
				<div style="margin-bottom:20px">
					<label class="item-label" ><span class="item-required">*</span>分组名称：</label>
					<input class="easyui-textbox" name="name" style="width:174px;" data-options="required:true">
				</div>
				<div style="margin-bottom:20px">
					<label class="item-label">包含规格：</label>
					<select class="easyui-combobox" id="metadataSelect" name="metadataCode" style="width:50%;" data-options="required:false,multiple:true" >
					</select>

				</div>
				<div style="margin-bottom:20px">
					<label class="item-label">权限人员：</label>
					<select class="easyui-combobox" id="userSelect" name="userId" style="width:50%;" data-options="required:false,multiple:true">
					</select>
				</div>
				<div style="margin-bottom:20px">
					<label class="item-label" >描述：</label>
					<input class="text" name="description" style="width:50%;min-height:90px;" data-options="multiline:true">
				</div>

			</form>
			<div style="margin-left: 100px;margin-bottom: 20px;">
				<a href="javascript:void(0)" class="easyui-linkbutton c8" onclick="submitForm()" style="width:80px">保存</a>
				<a href="javascript:void(0)" class="easyui-linkbutton" onclick="cancelForm()" style="width:80px">取消</a>
			</div>
		</div>

	</div>
</div>
</body>
<script type="text/javascript">
    var id = getQueryParameter("id");categoryCode=getQueryParameter("categoryCode");
    $(function(){
        setNav();
        $.when(initMetadataSelect("metadataSelect"),initUserSelect("userSelect"),initCategorySelect("categorySelect")).done(function(){
            initFormData();
        });
    });
    function initFormData(){
        if(id){
            $.ajax({
                url:config.url +"/mPms/"+id,
                type:'get',
                data:{},
                xhrFields:{withCredentials:true},
                success:function(data){
                    //填充表单
                    if(data){
                        var item = changeProperty2String(data);
                        if(!item.values){
                            item.values = [];
                        }
                        $("#ff").form("load",item);
                    }
                },
                error:function(){
                }
            });
        }
        $("#categorySelect").combobox("select",categoryCode);
        $("#categorySelect").combobox('readonly', true);
    }
    function initMetadataSelect(selectId, onchange, callback){
        var dtd = $.Deferred();
        $.ajax({
            url:config.url +"/categoryMetadata/getMetadataAll?categoryCode="+categoryCode,
            type:"get",
            xhrFields:{withCredentials:true},
            success:function (data) {
                if(data){
                    // data.unshift({"name":"-请选择-","code":""});
                    $("#"+selectId).combobox({
                        "textField":"name",
                        "valueField":"code",
                        "panelHeight":150,
                        "data":data,
                        onChange:function(newValue,oldValue){
                            var val = $(this).combobox("getText");
                            var textField = $(this).combobox("options").textField;
                            var matchSize = $(this).combobox("getData").filter(function(item){
                                return item[textField]==val;
                            }).length;
                            if(matchSize!=1){
                                return false;
                            }
                            if(newValue=="")return false;
                            if(onchange && typeof onchange === 'function'){
                                onchange.call(this,newValue);
                            }
                        }
                    });
                    callback && typeof(callback)==='function' && callback();
                }
                dtd.resolve();
            },
            error:function(XMLHttpRequest,textStatus,errorThrown){
                dtd.resolve();
            }
        })
        return dtd.promise();
    }
    function initUserSelect(selectId, onchange, callback){
        var dtd = $.Deferred();
        $.ajax({
            url:config.url +"/user/getByUsername",
            type:"get",
            xhrFields:{withCredentials:true},
            success:function (data) {
                if(data){
                    // data.unshift({"name":"-请选择-","code":""});
                    $("#"+selectId).combobox({
                        "textField":"name",
                        "valueField":"code",
                        "panelHeight":150,
                        "data":data,
                        onChange:function(newValue,oldValue){
                            var val = $(this).combobox("getText");
                            var textField = $(this).combobox("options").textField;
                            var matchSize = $(this).combobox("getData").filter(function(item){
                                return item[textField]==val;
                            }).length;
                            if(matchSize!=1){
                                return false;
                            }
                            if(newValue=="")return false;
                            if(onchange && typeof onchange === 'function'){
                                onchange.call(this,newValue);
                            }
                        }
                    });
                    callback && typeof(callback)==='function' && callback();
                }
                dtd.resolve();
            },
            error:function(XMLHttpRequest,textStatus,errorThrown){
                dtd.resolve();
            }
        })
        return dtd.promise();
    }

    function submitForm(){
        $("#ff").form("submit",{
            onSubmit:function(){
                var valid = $(this).form('enableValidation').form('validate');
                if(valid){
                    var formData = getFormData(document.getElementById("ff"));
                    var method = "POST";
                    if(id){
                        method = "PATCH";
                        formData.id = id;
                    }
                    var metadataCode = $("#metadataSelect").combobox('getValues');
                    formData.metadataCode=metadataCode;
                    var userId = $("#userSelect").combobox('getValues');
                    formData.userId=userId;
                    $.ajax({
                        url:config.url+"/mPms",
                        type:method,
                        data:JSON.stringify(formData),
                        dataType:"json",
                        contentType:"application/json;charset=UTF-8",
                        xhrFields:{withCredentials:true},
                        beforeSend:function(){
                            $.messager.progress({
                                title:"请稍等",
                                interval:50,
                                text:"操作进行中..."
                            });
                        },
                        complete:function(){
                            $.messager.progress("close");
                        },
                        success:function(data){
                            if(data){
                                $.messager.alert("提示","操作成功","info",function () {
                                    window.parent.closeCurrentTab(true);
                                });
                            }
                        },
                        error:function(data){
                            if(data){
                                $.messager.alert("提示",data.responseJSON.message||"操作失败","error");
                            }
                        }
                    });
                    return false;
                }
                return false;
            }
        })
    }
    function cancelForm(){
        window.parent.closeCurrentTab();
    }

</script>
</html>